#include "global.h"

int kwait(int* status)
{
	if (running->numChildren > 0)
	{	
		while (1)
		{
			int i;
			for (i = 0; i < NUM_PROCS; ++i)
			{
				if ((processes[i].parentProcessID == running->processID) &&
					(processes[i].status == ZOMBIE))
				{
					printf("  * P%d found a ZOMBIE child (P%d)! Burying him...\n",
						running->processID, processes[i].processID);
					--running->numChildren;
					*status = processes[i].exitValue;
					processes[i].status = FREE;
					enqueue(&processes[i], &freeQueue);
					return processes[i].processID;
				}
			}
			sleep(running);
		}
	}
	printf("  * I have no children, what am I to wait for!?\n");
	return -1;
}
